#include <iostream>
#include <winsock.h>
#include "INET_Addr.h"
#include "SOCK_Connector.h"
#include "SOCK_Stream.h"

using namespace std;

#define _port 3492
#define _address "127.0.0.1"
#define _len 50

int main()
{
	char ipaddress[50] = "";
	const char localHost[10] = _address;
	char sendString[_len];

	char* selectedAddress;
	char inputName[50];

	cout << "CLIENT" << endl << endl;

	//hostent* localHost;
	//char* localIP;

	//localHost = gethostbyname("");
	//localIP = inet_ntoa (*(struct in_addr *)*localHost->h_addr_list);

	while(true)
	{
		selectedAddress = ipaddress;
		cout << "Write IP address (type L to use local host): " << endl;
		cin >> selectedAddress;

		if(*selectedAddress == 'l' || *selectedAddress == 'L')
			selectedAddress = (char*)localHost;

		cout << "Initializing..." << endl;

		INET_Addr address = INET_Addr(_port, selectedAddress);
		SOCK_Connector connector = SOCK_Connector();
		SOCK_Stream streamer = SOCK_Stream();

		cout << "Input client name: " << endl;
		cin >> inputName;

		cout << "Connecting to " << selectedAddress << endl;
	
		if(connector.connect(address, streamer) == 0)
		{
			cout << "Connected!" << endl << endl;
			streamer.send(inputName,0);
		}

		else
		{
			cout << "Could not connect. Resetting..." << endl << endl;
		}
	}
}